package com.example.demo.配置类;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class 消息队列配置 {
    public static final String 延时交换机 = "延时交换机";
    public static final String 死信交换机 = "死信交换机";
    public static final String 延时队列="延时队列";
    public static final String 死信队列="死信队列";
    public static final String 延时队列routingkey="延时队列routingkey";
    public static final String 死信队列routingkey="死信队列routingkey";

    @Bean("delayExchange")
    public DirectExchange directExchange(){
        return new DirectExchange(延时交换机);
    }

    @Bean("dlxExchange")
    public DirectExchange dlxExchange(){
        return new DirectExchange(死信交换机);
    }

    @Bean("dlxQueue")
    public Queue dlxQueue(){
        return new Queue(死信队列);
    }
    @Bean
    public Queue delayQueue(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",死信交换机);
        map.put("x-dead-letter-routing-key",死信队列routingkey);
        map.put("x-message-ttl",15000);
        return QueueBuilder.durable(延时队列).withArguments(map).build();
    }
    @Bean
    public Binding dalayBinding(@Qualifier("dlxQueue") Queue queue,
                                @Qualifier("dlxExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with(死信队列routingkey);
    }
    @Bean
    public Binding dlxBinding(@Qualifier("delayQueue") Queue queue,
                              @Qualifier("delayExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with(延时队列routingkey);
    }
}
